package com.song.mall.cart.controller;

import com.song.common.constant.AuthServerConstant;
import com.song.mall.cart.interceptor.CartInterceptor;
import com.song.mall.cart.service.CartService;
import com.song.mall.cart.vo.Cart;
import com.song.mall.cart.vo.CartItem;
import com.song.mall.cart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author 宋超
 * HP - the login name of the current user.
 * 2021/10/6 - the current system date.
 * 20:53 - the current system time.
 */
@Controller
public class CartController {


    @Autowired
    private CartService cartService;

    /**
     * 获取到当前用户的所有购物项
     */
    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItem> getCurrentUserCartItems(){
        return cartService.getUserCartItems();//获取用户的购物车里面的数据
    }

    /**
     * 浏览器有一个cookie;user-key;标识用户身份，一个月后过期
     * 如果第一次使用jd的购物车功能。都会给一个临时的用户身份
     * 登录：session有
     * 没登录：按照cookie的user-key 来做
     * 第一次如果没有临时用户，帮忙创建一个临时用户
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        /*//快速得到用户信息
        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
        System.out.println(userInfoTo);*/
        Cart  cart =  cartService.getCart();//获取购物车
        model.addAttribute("cart",cart);
        return "cartList";
    }

    /**
     * 添加商品到购物车
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num,
                            RedirectAttributes ra) throws ExecutionException, InterruptedException { //参数：哪个商品,数量 throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId,num);
        ra.addAttribute("skuId",skuId);
        return "redirect:http://cart.mall.com/addToCartSuccess.html";
    }


    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,Model model){
        //根据商品id再查一次购物车商品数量有多少个
        CartItem item = cartService.getCartItem(skuId);     //获取某一个指定的购物项
        model.addAttribute("item",item);
        return "success";
    }

    /**
     *勾选购物车里面的购物项处理器
     * @param skuId
     * @param check
     * @return
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check")Integer check){
        /**
         * skuId :是要勾选的商品
         * check：是否勾选
         */
        cartService.checkItem(skuId,check);
        return "redirect:http://cart.mall.com/cart.html";
    }

    /**
     * 改变购物车当中商品的数量
     */
    //countItem
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,@RequestParam("num")Integer num){
        /**
         * skuId：改变哪个商品
         * num：改变的数量
         */
        cartService.changeItemCount(skuId,num);
        return "redirect:http://cart.mall.com/cart.html";
    }

    /**
     * 删除购物车当中某一个购物项的请求处理
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.mall.com/cart.html";

    }



}
